<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <script th:src="@{/layui.js}" charset="utf-8"></script>
    <link rel="stylesheet"  th:href="@{/css/layui.css}" media="all">
    <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
</head>
<body>
<br>

<table class="layui-hide" id="userTable" lay-filter="userTable"></table>

<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="getCheckData">获取选中行数据</button>
        <button class="layui-btn layui-btn-sm" lay-event="getCheckLength">获取选中数目</button>
        <button class="layui-btn layui-btn-sm" lay-event="isAll">验证是否全选</button>
        <button class="layui-btn layui-btn-sm" lay-event="add">添加</button>
    </div>
</script>
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>



<!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->
<script>
    var $,table;
    layui.use('table', function(){
        table = layui.table;
        $ = layui.jquery;
        table.render({
            elem: '#userTable'
            ,url:'/user/listJson'
            ,toolbar: '#toolbarDemo'
            ,title: '权限数据表'
            ,totalRow: true
            ,cols: [
                [
                {type: 'checkbox', fixed: 'left'}
                //,{field:'id', title:'ID', width:80, fixed: 'left', unresize: true, sort: true, totalRowText: '合计'}
                ,{field:'id', title:'ID', width:60}
                ,{field:'username', title:'用户名', width:150}
                    ,{field:'password', title:'密码', width:200}
                ,{field:'locked', title:'状态', width:300,templet:function(d){return d.locked=='0'?'正常':'锁住'}}
                ,{field:'roleNames', title:'角色', width:300}
                ,{fixed: 'right', title:'操作', toolbar: '#barDemo', width:150}
            ]
            ]
            ,page: true
            ,limit:7
            ,limits:[7,8,9,10]
            ,id:'userTable'
        });

        //工具栏事件
        table.on('toolbar(userTable)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id);
            switch(obj.event){
                case 'getCheckData':
                    var data = checkStatus.data;
                    layer.alert(JSON.stringify(data));
                    break;
                case 'getCheckLength':
                    var data = checkStatus.data;
                    layer.msg('选中了：'+ data.length + ' 个');
                    break;
                case 'isAll':
                    layer.msg(checkStatus.isAll ? '全选': '未全选')
                    break;
                case 'add':
                    //layer.msg('弹出添加页面');
                    parent.layer.open({
                        type:2,//2跳转到页面
                        offset: 'auto',
                        title:'添加新用户',
                        content:'/user/add',
                        area: ['600px', '500px'],
                        btn:'关闭全部',
                        btnAlign:'c',
                        yes:function () {
                            parent.layer.closeAll();
                        }
                    });
                    break;
            };
        });

        //监听工具条
        table.on('tool(userTable)', function(obj){ //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
            console.log(obj);
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）

            if(layEvent === 'del'){ //删除

            } else if(layEvent === 'edit'){ //编辑
                //弹出编辑页面
                parent.layer.open({
                    type:2,//2跳转到页面
                    offset: 'auto',
                    title:'【修改】用户的信息',
                    content:'/user/edit?userId='+obj.data.id,
                    area: ['600px', '500px'],
                    btn:'关闭全部',
                    btnAlign:'c',
                    yes:function () {
                        parent.layer.closeAll();
                    }
                });
            }
        });
    });



</script>

</body>
</html>